package com.example.spring_config.util;

import com.example.spring_config.mapper.Permission;

import java.util.*;

public class FormatUtil {

    /**
     * 把permission转换为父子关系的集合
     * @param lists 权限集合
     * @return 转换后的权限集合
     */
    public static List<Permission> formatData(Set<Permission> lists){
        List<Permission> list = new ArrayList<>();
        lists.forEach(t->list.add(t));

        Map<String,Permission> map = new HashMap<>();
        list.forEach(t->map.put(t.getId(),t));
        for (int i = 0; i < list.size(); i++) {
            Permission p = list.get(i);
            Permission permission = map.get(p.getPId());
            if(!p.getPId().equals("") && permission!=null){
                permission.getSonePermissionList().add(p);
                list.remove(p);
                i--;
            }
        }
        return list;
    }

}
